package com.ydy.user.exception;

import com.ydy.common.vo.ResultVo;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常捕捉
 **/
@RestControllerAdvice
public class ExceptionController {

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(RuntimeException.class)
    public ResultVo runException(RuntimeException e) {
        ResultVo<Object> objectResultVo = new ResultVo<>();
        objectResultVo.setCode(401);
        objectResultVo.setMsg(e.getMessage());
        return objectResultVo;
    }

    @ExceptionHandler(AuthorizationException.class)
    public ResultVo authErrorHandle(AuthorizationException e) {
        ResultVo<Object> objectResultVo = new ResultVo<>();
        objectResultVo.setCode(403);
        objectResultVo.setMsg(e.getMessage());
        return objectResultVo;
    }
}

